package cn.star.jots.records.service.impl;

import cn.star.jots.records.mapper.RecordsMapper;
import cn.star.jots.records.pojo.dto.RecordsQuery;
import cn.star.jots.records.pojo.dto.RecordsSaveParam;
import cn.star.jots.records.pojo.dto.RecordsUpdateParam;
import cn.star.jots.records.pojo.entity.Records;
import cn.star.jots.records.pojo.vo.RecordsVO;
import cn.star.jots.records.service.RecordsService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class RecordsServiceImpl implements RecordsService {

    @Autowired
    RecordsMapper recordsMapper;

    @Override
    public RecordsVO save(RecordsSaveParam recordsSaveParam) {
        log.debug("保存观看记录,参数:{}", recordsSaveParam);
        Boolean isExist = recordsMapper.isExist(recordsSaveParam);
        Records records = new Records();
        BeanUtils.copyProperties(recordsSaveParam, records);
        if (!isExist) {
            records.setCreateTime(new Date());
            recordsMapper.insert(records);
        }
        return recordsMapper.selectById(records.getId());
    }

    @Override
    public void update(RecordsUpdateParam recordsUpdateParam) {
        log.debug("更新观看记录,参数:{}", recordsUpdateParam);
        Records records = new Records();
        BeanUtils.copyProperties(recordsUpdateParam, records);
        int seconds = recordsUpdateParam.getSeconds().intValue();
        int time = recordsUpdateParam.getTime().intValue();
        records.setSeconds(seconds);
        records.setTime(time);
        records.setUpdateTime(new Date());
        recordsMapper.update(records);
    }

    @Override
    public List<Records> select(RecordsQuery recordsQuery) {
        log.debug("查询观看记录,参数:{}", recordsQuery);
        return recordsMapper.select(recordsQuery);
    }

    @Override
    public void delete(Long id) {
        log.debug("删除观看记录,参数:{}", id);
        recordsMapper.delete(id);
    }

}
